package com.stlm2.quartz;


import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

import java.lang.reflect.Method;

public class IJobDetailFactoryBean extends MethodInvokingJobDetailFactoryBean.StatefulMethodInvokingJob {
    private String targetObject;
    private String targetMethod;
    private ApplicationContext applicationContext;

    public String getTargetObject() {
        return targetObject;
    }

    public void setTargetObject(String targetObject) {
        this.targetObject = targetObject;
    }

    public String getTargetMethod() {
        return targetMethod;
    }

    public void setTargetMethod(String targetMethod) {
        this.targetMethod = targetMethod;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    protected void executeInternal(JobExecutionContext context)
            throws JobExecutionException {
        try {
            Object otargetObject = getApplicationContext()
                    .getBean(targetObject);
            Method method = null;
            try {
                method = otargetObject.getClass().getMethod(targetMethod,
                        JobExecutionContext.class);
                method.invoke(otargetObject, context);
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            }
        } catch (Exception e) {
            throw new JobExecutionException(e);
        }
    }
}
